package com.likou;

import java.util.Deque;
import java.util.LinkedList;

/**
 * Author: 陈桂明
 * Date: 2025/9/9 17:07
 * Description: q98
 */
public class q98 {


    // 栈
    public boolean isValidBST(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        TreeNode p = root;
        TreeNode mark = null;

        while (p != null || !deque.isEmpty()) {
            while (p != null) {
                deque.push(p);
                p = p.left;
            }

            TreeNode pop = deque.pop();
            if (mark != null && mark.val >= pop.val) {
                return false;
            }
            mark = pop;

            p = pop.right;
        }

        return true;

    }


//    // 递归
//    public boolean isValidBST(TreeNode root) {
//
//        return check(root, Long.MIN_VALUE, Long.MAX_VALUE);
//    }
//
//    private boolean check(TreeNode node, Long min, Long max) {
//        if (node == null) {
//            return true;
//        }
//        if (node.val <= min || node.val >= max) {
//            return false;
//        }
//
//        return check(node.left, min, (long) node.val) && check(node.right, (long) node.val, max);
//    }
}
